import store from '@/store/index.js'
import { httpUrl } from '@/utils/options.js'
let token = uni.getStorageSync('token')
let baseUrl = httpUrl



function http(obj) {
  let token = uni.getStorageSync('token') //用户登录token
  let info = uni.getStorageSync('userInfo') || {
    id: ''
  }
  let nurseId = info.id || ''
  return new Promise((resolve, reject) => {
    console.log('传参数', obj)
    // 基本的配置
    var setting = {
      url: baseUrl + obj.url, //URL地址
      data: obj.data, //参数
      method: obj.method.toUpperCase() || 'GET', //默认是get方法
      header: {
        // 'nurseId': nurseId,
        'content-type': 'application/x-www-form-urlencoded',
        'content-type': 'application/json;charset=UTF-8'
      },
      timeout: 120000 //3分钟
    }

    // 如果用户登录了添加上token
    if (token) {
      setting.header.token = token
    }
    // console.log(store.state.Nurseid);
    let nurseId = store.state.Nurseid
    if (nurseId) {
      setting.header.nurseId = nurseId
    } else {
      setting.header.nurseId = ''
    }
    // 请求数据 请求成功失败在这里处理
    uni.request({
      ...setting,
      success(res) {
        console.log('request.js配置', setting, res)
        if (res.statusCode == 200) {
          if (res.data.success) {
            resolve(res.data)
          } else {
            if (res.data.codeMsg) {
              if (res.data.codeMsg.msg) {
                console.log('错误消息================')
                // 错误处理
                // #ifdef APP-PLUS
                plus.nativeUI.toast(res.data.codeMsg.msg)
                // #endif
                // #ifdef H5
                uni.showToast({
                  title: res.data.codeMsg.msg,
                  icon: 'none'
                })
                // #endif
              }
              reject(res.data)
              return
            }
            reject(res.data)
          }
        } else {
          console.log('请求失败1=============', JSON.stringify(res), JSON.stringify(setting))
          resolve(false)

          rejectError(res)
        }
      },
      fail(err) {
        console.log('请求失败2=============', JSON.stringify(err), JSON.stringify(obj))
        reject(err)
        rejectError(err)
      }
    })

    // 清除缓存token过期
    async function rejectError(err) {
      uni.hideLoading()
      // #ifdef APP-PLUS
      plus.nativeUI.closeWaiting()
      // #endif
      let res = await store.dispatch('isNetWork')
      if (!res) return //没网了

      if (err.statusCode == 500) {
        //#ifdef APP-PLUS
        plus.nativeUI.closeWaiting()
        plus.nativeUI.toast('服务器开小差了,请稍后再试~')
        //#endif
        // #ifndef APP-PLUS
        uni.showToast({
          title: '服务器开小差了,请稍后再试~',
          icon: 'none'
        })
        // #endif
        return
      }
      if (err.statusCode == 401 && store.state.token) {
        console.log('没有权限', store.state.token)
        console.log(err, '错误信息')
        store.state.token = ''
        // #ifdef APP-PLUS
        let rst = await store.dispatch('preLogin')
        console.log(rst, '预登陆信息.......')
        if (rst) {
          uni.reLaunch({
            url: '../../login-register/one-key-login/one-key-login?type=1'
          })
        } else {
          uni.reLaunch({
            url: '../../login-register/login/login?type=1'
          })
        }
        // #endif
        // #ifdef H5
        uni.reLaunch({
          url: '../../login-register/login/login'
        })
        // #endif
        // store.dispatch('login')
      }
	  console.log("错误",err)
      if (err.statusCode == 504 && store.state.token) {
        uni.showToast({
          title: '该账户已在其他设备登录',
          icon: 'none',
          duration: 600
        })
        store.state.token = ''
        // #ifdef APP-PLUS
        let rst = await store.dispatch('preLogin')
        console.log(rst, '预登陆信息.......')
        if (rst) {
          uni.reLaunch({
            url: '../../login-register/one-key-login/one-key-login?type=1'
          })
        } else {
          uni.reLaunch({
            url: '../../login-register/login/login?type=1'
          })
        }
        // #endif
        // #ifdef H5
        uni.reLaunch({
          url: '../../login-register/login/login'
        })
        // #endif
      }
    }
  })
}
export default http
